var dgUtil = {
    partIndex: 0,
    existNextPage: 0,
    total: 0,
    partIndex: 0,
    checkColumns: function (sql) {

        // var sql="select id,pic,ooo from pic where 111=aaa and 111 in select.....";
        var columns = [];
        if (checkSqlInject(sql)) {
            var sqlre = sql;
            var sqlre_temp = null;
            do {
                sqlre_temp = sqlre;
                sqlre = sqlre.replace("select", "");
            }
            while (sqlre_temp != sqlre)
            do {
                sqlre_temp = sqlre;
                sqlre = sqlre.replace(" ", "").replace(" ", "");
            }
            while (sqlre_temp != sqlre)
            sql = sqlre;

            var temp = [];
            temp = sql.split("from");
            if (temp[0].indexOf(",") == -1) {
                columns[0] = temp[0];
                return columns;
            }
            else {

                columns = temp[0].split(",");

                for (i = 0; i < columns.length; i++) {
                    if (columns[i].indexOf(".") != -1) {
                        var tempdot = [];
                        tempdot = columns[i].split(".");
                        columns[i] = tempdot[tempdot.length - 1];


                    }
                }


                return columns;
            }
        }
        else {
            columns[0] = "illegal";
            return columns;
        }
    },
    makeColumns: function () {
        var datatLoad = [];
        var tempcolumms;
        tempcolumms = JSON.stringify(data[0]).split(",");
        for (var m = 0; m < tempcolumms.length; m++) {
            colums[m] = tempcolumms[m].split(":")[0].replace("\"", "").replace("\"", "").replace("{", "").replace("}", "");
        }
        var columnsJsonArray = [];
        for (var i = 0; i < colums.length; i++) {
            columnsJson.push({"field": colums[i], "title": colums[i]});
        }
    },
    bindKeyBoard: function (id) {
        $.extend($.fn.datagrid.methods, {
            keyCtr: function (jq) {
                return jq.each(function () {
                    var grid = $(this);
                    grid.datagrid('getPanel').panel('panel').attr('tabindex', 1).bind('keydown', function (e) {
                        switch (e.keyCode) {
                            case 38: // up
                                var selected = grid.datagrid('getSelected');
                                if (selected) {
                                    var index = grid.datagrid('getRowIndex', selected);
                                    grid.datagrid('selectRow', index - 1);

                                } else {
                                    var rows = grid.datagrid('getRows');
                                    grid.datagrid('selectRow', rows.length - 1);
                                }
                                dataGirdClickListener(grid.datagrid('getRowIndex', selected), grid.datagrid('getSelected'))
                                break;
                            case 40: // down
                                var selected = grid.datagrid('getSelected');
                                if (selected) {
                                    var index = grid.datagrid('getRowIndex', selected);
                                    grid.datagrid('selectRow', index + 1);
                                } else {
                                    grid.datagrid('selectRow', 0);
                                }
                                dataGirdClickListener(grid.datagrid('getRowIndex', selected), grid.datagrid('getSelected'))

                                break;
                        }
                    });
                });
            }
        });
        $("id").datagrid({}).datagrid("keyCtr");
    },
    dataGirdClickListener: function dataGirdClickListener(rowIndex, row) {
        //var rows = $(this).datagrid('getRows');
        //var row = rows[rowIndex];
        //var id = row.id;
        if (returnCitySN["cip"].slice(0, 10) == "202.101.92") {

            value = ftpsvr + row.picname.slice(7);

        }
        else {
            value = "ftp://221.13.4.111/fast" + row.picname.slice(7);
        }
        $("#pulsarpic").attr("src", value);
    },
    queryPic_info: function queryPic_info(girdDomId, url) {
        $.messager.progress({
            title: 'please wait',
            msg: 'loding...'
        })
        $(girdDomId).show();
        $.ajax({
            type: "post",
            contentType: "application/json",
            url: "url",
            data: sqlSend,
            dataType: "json",
            success: function onSucc(data) {
                $.messager.progress('close');
                deleteEmptyProperty(data);
                var partMessage = data.pop();
                part = partMessage.part;
                existNextPage = partMessage.existNextPart;
                total = partMessage.size;
                document.getElementById('pagenum').innerHTML = "第" + (Number(partIndex) + 1) + "页";
                document.getElementById('partnum').innerHTML = "共" + (Number(part)) + "页";
                document.getElementById('totalnum').innerHTML = "共" + total + "条记录";

                // eval("["+JSON.stringify(columnsJson)+"]");

                $("id").datagrid({
                    columns: eval("[" + JSON.stringify(makeColumns()) + "]"),
                    data: data,
                    pagination: false,
                    singleSelect: true,
                    //pageSize:100,
                    onClickRow: dataGirdClickListener
                });
                $("id").datagrid('hideColumn', 'picname');
                $("id").datagrid({}).datagrid("keyCtr");
            },
            error: function onErr() {
                $.messager.progress('close');
                alert("请求失败");
            }
        });

        // } else {
        // }
    },


    deleteEmptyProperty: function deleteEmptyProperty(object) {
        for (var i in object) {
            var value = object[i];
            if (typeof value === 'object') {
                if (Array.isArray(value)) {
                    if (value.length == 0) {
                        delete object[i];
                        // console.log('delete Array', i);
                        continue;
                    }
                }
                deleteEmptyProperty(value);
                if (isEmpty(value)) {
                    // console.log('isEmpty true', i, value);
                    delete object[i];
                    //  console.log('delete a empty object');
                }
            } else {
                if (value === '' || value === null || value === undefined) {
                    delete object[i];
                    // console.log('delete ', i);
                } else {
                    //  console.log('check ', i, value);
                }
            }
        }
    },
    isEmpty: function isEmpty(object) {
        for (var name in object) {
            return false;
        }
        return true;
    },


    //page
    lodeNextPage: function lodeNextPage() {
        if (Number(existNextPage)) {
            partIndex++;
            var sql = $('#queryCommandInput').val();
            var sqlJson = [];
            sqlJson.push({"tag": "sql", "sql": sql, "queryIndex": partIndex});
            // sqlJson.push({"sql":sql});
            var sqlSend = JSON.stringify(sqlJson);
            $.ajax({
                type: "post",
                contentType: "application/json",
                url: "/picSqlQuery",
                data: sqlSend,
                dataType: "json",
                success: function onSucc(data) {
                    deleteEmptyProperty(data);
                    var partMessage = data.pop();
                    $("#datagird").datagrid({data: data});
                    part = partMessage.part;
                    existNextPage = partMessage.existNextPart;
                    total = partMessage.size;

                    document.getElementById('pagenum').innerHTML = "第" + (Number(partIndex) + 1) + "页";
                    document.getElementById('partnum').innerHTML = "共" + (Number(part)) + "页";

                    document.getElementById('totalnum').innerHTML = "共" + total + "条记录";
                }
            });
        }
        else
            alert("这是最后一页啦~");
    },
    lodePrePage: function lodePrePage() {
        if (Number(partIndex) > 0) {
            partIndex--;
            var sql = $('#queryCommandInput').val();
            var sqlJson = [];
            sqlJson.push({"tag": "sql", "sql": sql, "queryIndex": partIndex});
            // sqlJson.push({"sql":sql});
            var sqlSend = JSON.stringify(sqlJson);
            $.ajax({
                type: "post",
                contentType: "application/json",
                url: "/picSqlQuery",
                data: sqlSend,
                dataType: "json",
                success: function onSucc(data) {
                    deleteEmptyProperty(data);
                    var partMessage = data.pop();
                    $("#datagird").datagrid({data: data});
                    part = partMessage.part;
                    existNextPage = partMessage.existNextPart;
                    total = partMessage.size;

                    document.getElementById('pagenum').innerHTML = "第" + (Number(partIndex) + 1) + "页";
                    document.getElementById('partnum').innerHTML = "共" + (Number(part)) + "页";
                    document.getElementById('totalnum').innerHTML = "共" + total + "条记录";

                }
            });
        }

        else {
            alert("这是第一页啦~");

        }
    }

}